﻿namespace ClassyBlog.Web.Specs.Unit
{
    using System.Collections.Generic;

    using Machine.Specifications;

    using DomainObjects;

    public class ViewModelTestDouble : ViewModel
    {
        public string Title { get; set; }
    }

    [Subject(typeof(ViewModel))]
    public class when_view_model_blog_is_set
    {
        static Blog blog;
        static ViewModelTestDouble viewModel;

        Establish context = () =>
        {
            blog = new Blog();
            viewModel = new ViewModelTestDouble();
        };

        Because of = () => viewModel.SetBlog(blog);

        It should_return_the_same_blog = () =>
            viewModel.Blog.ShouldBeTheSameAs(blog);
    }

    [Subject(typeof(ViewModel))]
    public class when_getting_dynamic_property_of_view_model
    {
        static ViewModelTestDouble viewModel;
        static dynamic dynamicViewModel;

        static string property;

        Establish context = () =>
        {
            viewModel = new ViewModelTestDouble();

            dynamicViewModel = viewModel;

            dynamicViewModel.DummyProperty = "DynamicValue";
        };

        Because of = () => property = dynamicViewModel.DummyProperty;

        It should_be_able_to_get = () => property.ShouldEqual("DynamicValue");
    }

    [Subject(typeof(ViewModel))]
    public class when_getting_regular_property_as_dynamic_property_of_view_model
    {
        static ViewModelTestDouble viewModel;
        static dynamic dynamicViewModel;

        static string title;

        Establish context = () =>
        {
            viewModel = new ViewModelTestDouble
                            {
                                Title = "Kazi Manzur Rashid"
                            };

            dynamicViewModel = viewModel;
        };

        Because of = () => title = dynamicViewModel.Title;

        It should_be_able_to_get = () =>
            title.ShouldEqual("Kazi Manzur Rashid");
    }

    [Subject(typeof(ViewModel))]
    public class when_setting_regular_property_as_dynamic_property_of_view_model
    {
        static ViewModelTestDouble viewModel;
        static dynamic dynamicViewModel;
        static string title;

        Establish context = () =>
        {
            viewModel = new ViewModelTestDouble();
            dynamicViewModel = viewModel;

            dynamicViewModel.Title = "Kazi Manzur Rashid";
        };

        Because of = () => title = viewModel.Title;

        It should_be_able_to_set = () =>
            title.ShouldEqual("Kazi Manzur Rashid");
    }

    [Subject(typeof(ViewModel))]
    public class when_view_model_dynamic_property_names_is_accessed
    {
        static ViewModelTestDouble viewModel;
        static IEnumerable<string> dynamicPropertyNames;

        Establish context = () =>
        {
            viewModel = new ViewModelTestDouble();
            viewModel.SetDynamicProperty("foo", "bar");
        };

        Because of = () => dynamicPropertyNames = viewModel.DynamicPropertyNames;

        It should_return_from_dynamic_property = () =>
            dynamicPropertyNames.ShouldContain("foo");
    }
}